package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.dto.ProductDto;
import com.woniu.entity.Product;
import com.woniu.service.CategoryService;
import com.woniu.service.ProductService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;

    /**？
     *
     * Pronduct dto查询
     */
    @GetMapping()
    public ResponseUtil findAllProduct( ProductDto productDto
            , @RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo all = productService.findAll(productDto, pageNum, pageSize);
        return ResponseUtil.get(ResponseEnum.OK,all);
    }
    // 图片上传路径
    private static final String UPLOAD_PATH = "D:/upload/product/";



    /**
     * 删除商品
     */
    @DeleteMapping("/{productId}")
    public ResponseUtil delete(@PathVariable Integer productId) {
        try {
            int result = productService.delete(productId);
            if (result > 0) {
                return ResponseUtil.get(ResponseEnum.SUCCESS);
            } else {
                return ResponseUtil.get(ResponseEnum.FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("删除商品失败");
        }
    }

    /**
     * 添加商品
     */
    @PostMapping("/add")
    public ResponseUtil add(@RequestBody Product product) {
        try {
            int result = productService.add(product);
            if (result > 0) {
                return ResponseUtil.get(ResponseEnum.SUCCESS);
            } else {
                return ResponseUtil.get(ResponseEnum.FAIL);
            }
        } catch (RuntimeException e) {
            return ResponseUtil.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("添加商品失败");
        }
    }
    /**
     * 根据ID查询商品
     */
    @GetMapping("/{productId}")
    public ResponseUtil findByProductId(@PathVariable Integer productId) {
        try {
            Product product = productService.findByProductId(productId);
            return ResponseUtil.success(ResponseEnum.SUCCESS, product);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("查询商品失败");
        }
    }
    /**
     * 更新商品
     */
    @PutMapping("/update")
    public ResponseUtil update(@RequestBody Product product) throws InterruptedException {
        return productService.update(product);
    }
    /**
     * 查询所有商品
     */
    @GetMapping("/all")
    public ResponseUtil findAll() {
        return  productService.findAll2();
    }
    /**
     * 图片上传
     */
    @PostMapping("/upload")
    public ResponseUtil upload(MultipartFile file) {
        try {
            // 创建上传目录
            File uploadDir = new File(UPLOAD_PATH);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }

            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + suffix;

            // 保存文件
            File dest = new File(UPLOAD_PATH + fileName);
            file.transferTo(dest);

            // 返回图片访问路径
            return ResponseUtil.success(ResponseEnum.SUCCESS, "/upload/product/" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseUtil.error("图片上传失败：" + e.getMessage());
        }
    }
    /**
     * 前端调用此接口获取过期商品列表
     */
    @GetMapping("/expired")
    public ResponseUtil getExpiredProducts() {
        // 调用服务层获取过期商品列表
        List<Product> expiredList = productService.getExpiredProducts();
        return ResponseUtil.get(ResponseEnum.OK, expiredList);
    }

    @GetMapping("/findAll")
    public ResponseUtil findAll1(){

        return productService.findAllVo();
    }

}
